package Q2.view;

import Q2.dao.StudentDao;
import Q2.dao.impl.StudentDaoMySQLImpl;
import Q2.pojo.Student;
import utils.JDBCUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.sql.Blob;

/**
 * Student view
 */
public class StudentView extends JFrame {

    private JPanel contentPane;
    private JTextField txtNo;
    private JLabel lblNo;
    private JLabel lblName;
    private JLabel lblPhoto;
    private JLabel lblAge;
    private JLabel lblBirthday;

    /**
     * Create the student show frame
     */
    public StudentView() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 586, 355);
        contentPane = new JPanel();
        contentPane.setMinimumSize(new Dimension(100, 10));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);
        panel.setLayout(new GridLayout(0, 3, 0, 0));

        JLabel lblNewLabel = new JLabel("学号");
        panel.add(lblNewLabel);

        txtNo = new JTextField();
        txtNo.setText("201901030150");
        panel.add(txtNo);
        txtNo.setColumns(10);

        JButton btnNewButton = new JButton("查找");
        btnNewButton.addActionListener(e -> find());
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(null);

        lblNo = new JLabel("New label");
        lblNo.setBounds(84, 10, 54, 15);
        panel_1.add(lblNo);

        JLabel label = new JLabel("学号");
        label.setBounds(10, 10, 54, 15);
        panel_1.add(label);

        JLabel label_1 = new JLabel("姓名");
        label_1.setBounds(10, 35, 54, 15);
        panel_1.add(label_1);

        lblName = new JLabel("New label");
        lblName.setBounds(84, 35, 54, 15);
        panel_1.add(lblName);

        JLabel label_3 = new JLabel("年龄");
        label_3.setBounds(10, 65, 54, 15);
        panel_1.add(label_3);

        lblAge = new JLabel("New label");
        lblAge.setBounds(84, 65, 54, 15);
        panel_1.add(lblAge);

        JLabel label_5 = new JLabel("出生日期");
        label_5.setBounds(10, 93, 54, 15);
        panel_1.add(label_5);

        lblBirthday = new JLabel("New label");
        lblBirthday.setBounds(84, 93, 54, 15);
        panel_1.add(lblBirthday);

        lblPhoto = new JLabel("New label");
        lblPhoto.setBounds(191, 10, 359, 263);
        panel_1.add(lblPhoto);

    }

    void find() {
        final String number = txtNo.getText().trim();
        final StudentDao studentDao = new StudentDaoMySQLImpl(JDBCUtils.getConnection());
        final Student student = studentDao.findByStudentNumber(number);
        if (student == null) {
            JOptionPane.showMessageDialog(null, "未找到学生", "未找到", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        System.out.println(student);

        lblNo.setText(student.getNo());
        lblName.setText(student.getName());
        lblAge.setText("" + student.getAge());
        lblBirthday.setText(student.getBirthday().toString());

        Blob photo = student.getPhoto();
        if (photo != null) {
            try {
                Image image = ImageIO.read(photo.getBinaryStream());
                lblPhoto.setIcon(new ImageIcon(image));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                final StudentView frame = new StudentView();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
